package com.hannesdorfmann.mosby3.mvp.lce;

import com.hannesdorfmann.mosby3.mvp.MvpPresenter;
import com.hannesdorfmann.mosby3.mvp.MvpView;
import com.hannesdorfmann.mosby3.mvp.viewstate.ViewState;
import com.hannesdorfmann.mosby3.mvp.viewstate.lce.LceViewState;
import com.hannesdorfmann.mosby3.mvp.viewstate.lce.MvpLceViewStateFraction;
import com.hannesdorfmann.mosby3.sample.ResourceTable;
import com.hannesdorfmann.mosby3.sample.mvp.CountriesErrorMessage;
import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.agp.components.LayoutScatter;
import ohos.app.Context;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class MvpLceViewStateFractionTest {

    MvpLceViewStateFraction mvpLceViewStateFraction;
    Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
    LceViewState viewState;
    LayoutScatter scatter;

    @Before
    public void setUp() throws Exception {
        scatter = LayoutScatter.getInstance(context);
        viewState = new LceViewState() {
            @Override
            public void setStateShowContent(Object loadedData) {

            }

            @Override
            public void setStateShowError(Throwable e, boolean pullToRefresh) {

            }

            @Override
            public void setStateShowLoading(boolean pullToRefresh) {

            }

            @Override
            public boolean isLoadingState() {
                return false;
            }

            @Override
            public boolean isPullToRefreshLoadingState() {
                return false;
            }

            @Override
            public void apply(MvpView view, boolean retained) {

            }
        };
        mvpLceViewStateFraction = new MvpLceViewStateFraction() {
            @Override
            public Object getData() {
                return null;
            }

            @Override
            public void setViewState(ViewState viewState) {
            }

            @Override
            public ViewState createViewState() {
                return viewState;
            }

            @Override
            protected void initLceComponetID() {
                loadingViewID = ResourceTable.Id_loadingView;
                contentViewID = ResourceTable.Id_contentView;
                errorViewID = ResourceTable.Id_errorView;
            }

            @Override
            protected String getErrorMessage(Throwable e, boolean pullToRefresh) {
                return CountriesErrorMessage.get(e/*new Throwable("active test err2")*/, pullToRefresh, context);
            }

            @Override
            public MvpPresenter createPresenter() {
                return null;
            }

            @Override
            protected int getUIContent() {
                return ResourceTable.Layout_countries_list;
            }

            @Override
            public void setData(Object data) {

            }

            @Override
            public void loadData(boolean pullToRefresh) {

            }
        };
//        mvpLceViewStateFraction.onComponentAttached(LayoutScatter.getInstance(context), null, null);
        mvpLceViewStateFraction.setViewState(viewState);
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void showContent() {
        mvpLceViewStateFraction.mComponent = scatter.parse(mvpLceViewStateFraction.getUIContent(), null, false);
        mvpLceViewStateFraction.initCompoment();
        mvpLceViewStateFraction.showContent();
        assertNotNull(mvpLceViewStateFraction.getViewState());
    }

    @Test
    public void showError() {
        mvpLceViewStateFraction.mComponent = scatter.parse(mvpLceViewStateFraction.getUIContent(), null, false);
        mvpLceViewStateFraction.initCompoment();
        mvpLceViewStateFraction.showError(new Throwable("test active err"), false);
        assertNotNull(mvpLceViewStateFraction.getViewState());
    }

    @Test
    public void showLoading() {
        mvpLceViewStateFraction.mComponent = scatter.parse(mvpLceViewStateFraction.getUIContent(), null, false);
        mvpLceViewStateFraction.initCompoment();
        mvpLceViewStateFraction.showLoading(false);
        assertNotNull(mvpLceViewStateFraction.getViewState());
    }

    @Test
    public void onViewStateInstanceRestored() {
        mvpLceViewStateFraction.mComponent = scatter.parse(mvpLceViewStateFraction.getUIContent(), null, false);
        mvpLceViewStateFraction.initCompoment();
        mvpLceViewStateFraction.onViewStateInstanceRestored(false);
        assertNotNull(mvpLceViewStateFraction);
    }

    @Test
    public void onNewViewStateInstance() {
        mvpLceViewStateFraction.mComponent = scatter.parse(mvpLceViewStateFraction.getUIContent(), null, false);
        mvpLceViewStateFraction.initCompoment();
        mvpLceViewStateFraction.onNewViewStateInstance();
        assertNotNull(mvpLceViewStateFraction);
    }

}